package com.springmvc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.springmvc.entity.Items;

/**
 * @author Administrator
 * 实现controller接口的handler
 */
public class ItemsController1 implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//模拟数据
		//实际开发中，此处应该调用service层，获取数据
		List<Items> items = new ArrayList<>();
		
		Items items_1 = new Items();
		items_1.setName("笔记本");
		items_1.setPrice(5000f);
		items_1.setDetail("质量很好");
		
		Items items_2 = new Items();
		items_2.setName("iPad");
		items_2.setPrice(2800f);
		items_2.setDetail("质量很好，值得信赖");
		
		items.add(items_1);
		items.add(items_2);
		
		//返回ModelAndView对象
		ModelAndView modelAndView = new ModelAndView();
		
		//设置数据,相当于在servlet中使用 request.setAttribute("itemsList", items)
		modelAndView.addObject("itemsList", items);
		
		//指定页面视图（jsp页面）,告诉view对象逻辑视图文件的位置
		modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
		
		return modelAndView;
	}

}
